import sys
import os
from PIL import Image


def get_files(src_dir):

    return [os.path.join(src_dir, f) for f in os.listdir("src")]


def crop_image(image_path, rec):

    im = Image.open(image_path)
    im_cropped = im.crop(rec)
    basename = os.path.basename(image_path)

    target = f"dist/crop_{basename}"
    im_cropped.save(target)


def resize_image(image_path, rec):
    im = Image.open(image_path)
    im_resized = im.resize((rec[0], rec[1]))
    ext = os.path.basename(image_path).split(".")[1]
    target = f"{rec[0]}x{rec[1]}.{ext}"
    im_resized.save(target)


def save_as_ico(image_path):
    im = Image.open(image_path)
    im.save("logo.ico", format="ICO")


if __name__ == "__main__":
    src_dir = sys.argv[1]

    for f in get_files(src_dir):
        if f.endswith(".jpg"):
            rec = (520, 280, 4400, 3400)
            crop_image(f, rec)
